using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System;

namespace ASPNETVN.PORTAL.Modules.NEWS.Components
{
    public partial class Item
    {
        public int InsertToForum(int forumID, int userID)
        {
            string spName = "yaf_topic_save";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ForumID", DbType.String, forumID);
            db.AddInParameter(dbCommand, "@Subject", DbType.String, this._Title);
            db.AddInParameter(dbCommand, "@UserID", DbType.Int32, userID);
            db.AddInParameter(dbCommand, "@Message", DbType.String, this._Memo);
            db.AddInParameter(dbCommand, "@Priority", DbType.Int16, 0);
            db.AddInParameter(dbCommand, "@UserName", DbType.String, DBNull.Value);
            db.AddInParameter(dbCommand, "@IP", DbType.String, "127.0.0.1");
            db.AddInParameter(dbCommand, "@PollID", DbType.Int32, DBNull.Value);
            db.AddInParameter(dbCommand, "@Posted", DbType.DateTime, DateTime.Now);
            db.AddInParameter(dbCommand, "@Flags", DbType.Int32, 22);
            return db.ExecuteNonQuery(dbCommand);
        }


        public static DataSet SelectRelationItems(int id)
        {
            string spName = "p_NEWS_Item_SelectRelationItems";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ItemID", DbType.Int32, id);
            return db.ExecuteDataSet(dbCommand);
        }

        public static DataSet SelectBy_ModuleID(int moduleID)
        {
            string spName = "p_NEWS_Item_SelectBy_ModuleID";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", DbType.Int32, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }

        public static DataSet SelectBy_ModuleID_Status(int moduleID, string status)
        {
            string spName = "p_NEWS_Item_SelectBy_ModuleID_Status";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", DbType.Int32, moduleID);
            db.AddInParameter(dbCommand, "@Status", DbType.String, status);
            return db.ExecuteDataSet(dbCommand);
        }

        public static int IncreaseTotalViews(int itemID)
        {
            string spName = "p_NEWS_Item_IncreaseTotalViews";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ItemID", DbType.Int32, itemID);
            return db.ExecuteNonQuery(dbCommand);
        }

        public static int IncreaseTotalComments(int itemID)
        {
            string spName = "p_NEWS_Item_IncreaseTotalComments";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ItemID", DbType.Int32, itemID);
            return db.ExecuteNonQuery(dbCommand);
        }

        public static int Approve(int itemID)
        {
            string spName = "[dbo].[p_NEWS_Item_Approve]";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ID", DbType.Int32, itemID);
            return db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Get top items
        /// </summary>
        /// <param name="moduleID">Module ID</param>
        /// <param name="optionView">TopView | TopComment | None</param>
        /// <returns>Dataset</returns>
        public static DataSet GetTopItems(int moduleID, string optionView)
        {
            string spName = "[dbo].[p_NEWS_Item_GetTopItems]";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", DbType.Int32, moduleID);
            db.AddInParameter(dbCommand, "@OptionView", DbType.String, optionView);

            return db.ExecuteDataSet(dbCommand);
        }
    }
}